﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class StaffMoney
    {
        public int Add(Chain.Model.StaffMoney model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into StaffMoney(");
            builder.Append("StaffID,StaffTotalMoney,StaffOrderCode,StaffMemID,StaffGoodsID,StaffShopID,StaffCreateTime,StaffOrderDetailID,StaffType)");
            builder.Append(" values (");
            builder.Append("@StaffID,@StaffTotalMoney,@StaffOrderCode,@StaffMemID,@StaffGoodsID,@StaffShopID,@StaffCreateTime,@StaffOrderDetailID,@StaffType)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffID", SqlDbType.Int, 4), new SqlParameter("@StaffTotalMoney", SqlDbType.Money, 8), new SqlParameter("@StaffOrderCode", SqlDbType.VarChar, 50), new SqlParameter("@StaffMemID", SqlDbType.Int, 4), new SqlParameter("@StaffGoodsID", SqlDbType.Int, 4), new SqlParameter("@StaffShopID", SqlDbType.Int, 4), new SqlParameter("@StaffCreateTime", SqlDbType.DateTime), new SqlParameter("@StaffOrderDetailID", SqlDbType.Int, 4), new SqlParameter("@StaffType", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.StaffID;
            cmdParms[1].Value = model.StaffTotalMoney;
            cmdParms[2].Value = model.StaffOrderCode;
            cmdParms[3].Value = model.StaffMemID;
            cmdParms[4].Value = model.StaffGoodsID;
            cmdParms[5].Value = model.StaffShopID;
            cmdParms[6].Value = model.StaffCreateTime;
            cmdParms[7].Value = model.StaffOrderDetailID;
            cmdParms[8].Value = model.StaffType;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int StaffMoneyID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from StaffMoney ");
            builder.Append(" where StaffMoneyID=@StaffMoneyID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffMoneyID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StaffMoneyID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string StaffMoneyIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from StaffMoney ");
            builder.Append(" where StaffMoneyID in (" + StaffMoneyIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public int DeleteStaff(string strStaffOrderCode)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" delete from StaffMoney where StaffOrderCode=@StaffOrderCode");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffOrderCode", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = strStaffOrderCode;
            object obj2 = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (obj2 == null)
            {
                return 0;
            }
            return Convert.ToInt32(obj2);
        }

        public bool Exists(int StaffMoneyID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from StaffMoney");
            builder.Append(" where StaffMoneyID=@StaffMoneyID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffMoneyID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StaffMoneyID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select StaffMoneyID,StaffID,StaffTotalMoney,StaffOrderCode,StaffMemID,StaffGoodsID,StaffShopID,StaffCreateTime,StaffOrderDetailID,StaffType ");
            builder.Append(" FROM StaffMoney ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" StaffMoneyID,StaffID,StaffTotalMoney,StaffOrderCode,StaffMemID,StaffGoodsID,StaffShopID,StaffCreateTime,StaffOrderDetailID,StaffType ");
            builder.Append(" FROM StaffMoney ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.StaffMoneyID desc");
            }
            builder.Append(")AS Row, T.*  from StaffMoney T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select * from V_StaffMoney");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" Order by StaffOrderDetailID asc");
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("StaffMoneyID", "StaffMoney");
        }

        public Chain.Model.StaffMoney GetModel(int StaffMoneyID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 StaffMoneyID,StaffID,StaffTotalMoney,StaffOrderCode,StaffMemID,StaffGoodsID,StaffShopID,StaffCreateTime,StaffOrderDetailID,StaffType from StaffMoney ");
            builder.Append(" where StaffMoneyID=@StaffMoneyID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffMoneyID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StaffMoneyID;
            Chain.Model.StaffMoney money = new Chain.Model.StaffMoney();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["StaffMoneyID"] != null) && (set.Tables[0].Rows[0]["StaffMoneyID"].ToString() != ""))
            {
                money.StaffMoneyID = int.Parse(set.Tables[0].Rows[0]["StaffMoneyID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffID"] != null) && (set.Tables[0].Rows[0]["StaffID"].ToString() != ""))
            {
                money.StaffID = int.Parse(set.Tables[0].Rows[0]["StaffID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffTotalMoney"] != null) && (set.Tables[0].Rows[0]["StaffTotalMoney"].ToString() != ""))
            {
                money.StaffTotalMoney = decimal.Parse(set.Tables[0].Rows[0]["StaffTotalMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffOrderCode"] != null) && (set.Tables[0].Rows[0]["StaffOrderCode"].ToString() != ""))
            {
                money.StaffOrderCode = set.Tables[0].Rows[0]["StaffOrderCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["StaffMemID"] != null) && (set.Tables[0].Rows[0]["StaffMemID"].ToString() != ""))
            {
                money.StaffMemID = int.Parse(set.Tables[0].Rows[0]["StaffMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffGoodsID"] != null) && (set.Tables[0].Rows[0]["StaffGoodsID"].ToString() != ""))
            {
                money.StaffGoodsID = int.Parse(set.Tables[0].Rows[0]["StaffGoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffShopID"] != null) && (set.Tables[0].Rows[0]["StaffShopID"].ToString() != ""))
            {
                money.StaffShopID = int.Parse(set.Tables[0].Rows[0]["StaffShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffCreateTime"] != null) && (set.Tables[0].Rows[0]["StaffCreateTime"].ToString() != ""))
            {
                money.StaffCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["StaffCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffOrderDetailID"] != null) && (set.Tables[0].Rows[0]["StaffOrderDetailID"].ToString() != ""))
            {
                money.StaffOrderDetailID = int.Parse(set.Tables[0].Rows[0]["StaffOrderDetailID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffType"] != null) && (set.Tables[0].Rows[0]["StaffType"].ToString() != ""))
            {
                money.StaffType = int.Parse(set.Tables[0].Rows[0]["StaffType"].ToString());
            }
            return money;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM StaffMoney ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetStaffMoney(string strWhere)
        {
            string sQLString = " select * from V_GoodsReturn ";
            if (strWhere != "")
            {
                sQLString = sQLString + " where " + strWhere;
            }
            return DbHelperSQL.Query(sQLString);
        }

        public DataSet GetStaffMoneyByTime(DateTime starttime, DateTime endtime, string strwhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StartTime", SqlDbType.DateTime), new SqlParameter("@EndTime", SqlDbType.DateTime), new SqlParameter("@strWhere", SqlDbType.NVarChar, 0x7d0) };
            parameters[0].Value = starttime;
            parameters[1].Value = endtime;
            parameters[2].Value = strwhere;
            return DbHelperSQL.RunProcedure("MonthStaffTotalMoney", parameters, "#StaffTotalMoneyData");
        }

        public decimal GetStaffTotalMoney(string strSql)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" SELECT SUM(ISNULL(StaffTotalMoney,0)) FROM StaffMoney,StaffClass,Staff,SysShop ");
            if (strSql.Trim() != "")
            {
                builder.Append(" where " + strSql);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public decimal GetTotalStaffMoney(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select sum(TotalMoney) from V_StaffMoney ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public bool Update(Chain.Model.StaffMoney model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update StaffMoney set ");
            builder.Append("StaffID=@StaffID,");
            builder.Append("StaffTotalMoney=@StaffTotalMoney,");
            builder.Append("StaffOrderCode=@StaffOrderCode,");
            builder.Append("StaffMemID=@StaffMemID,");
            builder.Append("StaffGoodsID=@StaffGoodsID,");
            builder.Append("StaffShopID=@StaffShopID,");
            builder.Append("StaffCreateTime=@StaffCreateTime,");
            builder.Append("StaffOrderDetailID=@StaffOrderDetailID,");
            builder.Append("StaffType=@StaffType");
            builder.Append(" where StaffMoneyID=@StaffMoneyID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffID", SqlDbType.Int, 4), new SqlParameter("@StaffTotalMoney", SqlDbType.Money, 8), new SqlParameter("@StaffOrderCode", SqlDbType.VarChar, 50), new SqlParameter("@StaffMemID", SqlDbType.Int, 4), new SqlParameter("@StaffGoodsID", SqlDbType.Int, 4), new SqlParameter("@StaffShopID", SqlDbType.Int, 4), new SqlParameter("@StaffCreateTime", SqlDbType.DateTime), new SqlParameter("@StaffOrderDetailID", SqlDbType.Int, 4), new SqlParameter("@StaffType", SqlDbType.Int, 4), new SqlParameter("@StaffMoneyID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.StaffID;
            cmdParms[1].Value = model.StaffTotalMoney;
            cmdParms[2].Value = model.StaffOrderCode;
            cmdParms[3].Value = model.StaffMemID;
            cmdParms[4].Value = model.StaffGoodsID;
            cmdParms[5].Value = model.StaffShopID;
            cmdParms[6].Value = model.StaffCreateTime;
            cmdParms[7].Value = model.StaffOrderDetailID;
            cmdParms[8].Value = model.StaffType;
            cmdParms[9].Value = model.StaffMoneyID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int UpdateStaffMoney(string strOrderCode, int intGoodsID, decimal money)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" update StaffMoney set StaffTotalMoney =@StaffTotalMoney ");
            builder.Append(" where StaffOrderCode =@StaffOrderCode and StaffGoodsID=@StaffGoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffTotalMoney", SqlDbType.Money, 8), new SqlParameter("@StaffOrderCode", SqlDbType.VarChar, 50), new SqlParameter("@StaffGoodsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = money;
            cmdParms[1].Value = strOrderCode;
            cmdParms[2].Value = intGoodsID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

